//
//  DoctorFreeInfo.m
//  EcmDoctor
//
//  Created by 张进 on 15/7/16.
//  Copyright (c) 2015年 张进. All rights reserved.
//

#import "DoctorFreeInfo.h"
#import "NetWorkUtil.h"
#import "UserAskPic.h"

@implementation DoctorFreeInfo

+(Class)picUrl_class{
    return [UserAskPic class];
}

+(void)getFreeInfosByDoctorId:(NSString *)doctorId success:(void (^)(NSArray *objs))success
                      failure:(void (^)(NSError *error))failure{
    
    NetWorkBaseRequest *req = [[NetWorkBaseRequest alloc] init];
    
    req.methodName = @"getfreeinfo";
    req.moduleName = @"DoctorRS";
    //    [req setValue:@"4a7c9948199e4dcbbf07a0c4ba8cc79a" forKey:@"doctor_info_id"];
    [req setValue:doctorId forKey:@"doctor_info_id"];
    
    [NetWorkUtil requestWithPOST:req success:^(NetWorkBaseResponse *responseObject) {
        NSError *error = nil;
        
        //        NSError errorWithDomain:@"" code:-1 userInfo:nil]
        
        if (!responseObject.data) {
            success([[NSMutableArray alloc] init]);
            return;
        }
        
        NSData *data = [responseObject.data dataUsingEncoding:NSUTF8StringEncoding];
        NSArray *freeInfos = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
        
        
        if (error) {
            failure(error);
            return;
        }
        
        if (!freeInfos || freeInfos.count == 0) {
            success([[NSMutableArray alloc] init]);
            return;
        }
        
        NSLog(@"%@",responseObject.data);
        
        NSMutableArray *result = [[NSMutableArray alloc] init];
        for (NSDictionary *dic in freeInfos) {
            DoctorFreeInfo *freeInfo = [[DoctorFreeInfo alloc] initWithDictionary:dic];
            [result addObject:freeInfo];
        }
        success(result);
    } failure:^(NSError *error) {
        failure(error);
    } cache:false];
}

+(void)doctorFreeDetailWithaskId:(NSString *)askId success:(void (^)(DoctorFreeInfo *))success failure:(void (^)(NSError *))failure {
    NetWorkBaseRequest *req = [[NetWorkBaseRequest alloc] init];
    //请求参数
    
    req.moduleName = @"UserRS";
    req.methodName = @"getOnLineDetaileInfoByID";
    [req setValue:askId forKey:@"ask_info_id"];
    
    [NetWorkUtil requestWithPOST:req success:^(NetWorkBaseResponse *responseObject) {
        NSError *error = nil;
        
        if (responseObject.code.integerValue == 1) {
            if (!responseObject.data) {
                success(nil);
                return;
            }
            
            NSData *data = [responseObject.data dataUsingEncoding:NSUTF8StringEncoding];
            NSDictionary *userAttrs = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
            if (error) {
                failure(error);
                return;
            }
            
            if (!userAttrs || userAttrs.count == 0) {
                
                failure([NSError errorWithDomain:@"0" code:-1 userInfo:nil]);
                return;
            }
            DoctorFreeInfo *doctor = [[DoctorFreeInfo alloc]initWithDictionary:userAttrs];

            success(doctor);
            
        } else {
            
            error = [NSError errorWithDomain:responseObject.msg code:responseObject.code.integerValue userInfo:nil];
            failure(error);
        }
        
    } failure:^(NSError *error) {
        failure(error);
        
    } cache:NO];
}

@end
